package com.gugee.tiktok.data.consumer.blogger.core.service;

import cn.hutool.core.date.DateUtil;
import com.gugee.tiktok.common.model.AwemeScore;
import com.gugee.tiktok.common.model.mysql.Aweme;
import com.gugee.tiktok.common.model.dal.blogger.AwemeRepository;
import com.gugee.tiktok.common.model.mysql.BloggerMain;
import com.gugee.tiktok.common.model.spider.aweme.BloggerAwemesResponse;
import com.gugee.tiktok.data.common.msg.AwemeMessage;
import com.gugee.tiktok.data.consumer.blogger.config.DataConsumerConfig;
import com.gugee.tiktok.data.consumer.blogger.core.main.ESRefreshAwemeMain;
import com.gugee.tiktok.data.consumer.blogger.core.main.SaveBloggerAwemeMain;
import com.gugee.tiktok.data.consumer.blogger.core.main.SetAwemeScoreMain;
import com.gugee.tiktok.common.model.dto.BloggerAwemeExtendDto;
import com.gugee.tiktok.common.model.dto.BloggerRecordAwemeExtendDto;
import com.gugee.tiktok.data.consumer.blogger.core.spider.AwemeSpiderClient;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Slf4j
@Service
public class AwemeService {
    @Autowired
    AwemeRepository awemeRepository;

    @Autowired
    SaveBloggerAwemeMain saveBloggerAwemeMain;
    @Autowired
    SetAwemeScoreMain setAwemeScoreMain;
    @Autowired
    ESRefreshAwemeMain esRefreshAwemeMain;
    @Autowired
    DataConsumerConfig dataConsumerConfig;

    /**
     * 爬虫接口
     */
    @Autowired
    AwemeSpiderClient awemeSpiderClient;

    public BloggerAwemeExtendDto getBloggerAwemeExtendDto(long uid) {
        Optional<BloggerAwemeExtendDto> bloggerAwemeExtendDto = awemeRepository
                .getBloggerAwemeExtend(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(dataConsumerConfig.getLatestDays())));
        return bloggerAwemeExtendDto.isPresent() ? bloggerAwemeExtendDto.get() : null;
    }
    public BloggerRecordAwemeExtendDto getBloggerRecordAwemeExtendDto(long uid) {
        Optional<BloggerRecordAwemeExtendDto> bloggerRecordAwemeExtendDto = awemeRepository
                .getBloggerRecordAwemeExtend(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(dataConsumerConfig.getLatestDays())));
        return bloggerRecordAwemeExtendDto.isPresent() ? bloggerRecordAwemeExtendDto.get() : null;
    }

    public BloggerRecordAwemeExtendDto getBloggerRecordAwemeExtendDto(long uid,int day) {
        Optional<BloggerRecordAwemeExtendDto> bloggerRecordAwemeExtendDto = awemeRepository
                .getBloggerRecordAwemeExtend(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(day)));
        return bloggerRecordAwemeExtendDto.isPresent() ? bloggerRecordAwemeExtendDto.get() : null;
    }

    public BloggerRecordAwemeExtendDto getBloggerRecordForZeroTimeAwemeExtendDto(long uid,int day) {
        Long timStamp = DateUtil.beginOfDay(new Date()).getTime();
        LocalDateTime  localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timStamp), ZoneId.systemDefault()).minusDays(day);
        Optional<BloggerRecordAwemeExtendDto> bloggerRecordAwemeExtendDto = awemeRepository
                .getBloggerRecordAwemeExtend(uid, localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond());
        return bloggerRecordAwemeExtendDto.isPresent() ? bloggerRecordAwemeExtendDto.get() : null;
    }


    public List<Aweme> getBloggerAwemes(long uid) {
        List<Aweme> bloggerAweme = awemeRepository.findAwemesByoUidAndoCreateTimeGreaterThan(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(dataConsumerConfig.getLatestDays())));
        return bloggerAweme;
    }

    public List<Aweme> get24HourBloggerAwemes(long uid){
        List<Aweme> bloggerAweme = awemeRepository.findByUidAndBetween24Hour(uid);
        return bloggerAweme;
    }
    public List<Aweme> get30DayBloggerAwemes(long uid) {
        //调整为10天内视频 20200918 bby llf ,为评论而设置
        List<Aweme> bloggerAweme = awemeRepository.findAwemesByoUidAndoCreateTimeGreaterThan(uid, DateTimeUtils.second(LocalDateTime.now().minusDays(10)));
        return bloggerAweme;
    }

    public void refreshBloggerAweme(String bloggerType,long uid) {
        saveBloggerAwemeMain.handle(bloggerType,uid);
    }

    public void refreshBloggerAwemeForES(long uid){
        esRefreshAwemeMain.handle(uid);
    }

    public void refreshBloggerAwemeScore(AwemeScore tikTokScore){
        setAwemeScoreMain.handle(tikTokScore);

    }
    public void batchUpdateBlogger(List<BloggerMain> bloggerMains){

    }

    /**
     * 确认视频存在处理类
     * @param msg
     */
    public void awemeExistHandle(AwemeMessage msg) {
        BloggerAwemesResponse bloggerAwemesResponse;
        long awemeId = msg.getAwemeId();
        try {
            //爬数据
            bloggerAwemesResponse = awemeSpiderClient.getAwemeDetailList( awemeId+ "");
        } catch (Exception e) {
            log.error("CheckAwemeExist error,调用接口爬取视频详情接口失败，awemeId: {},异常：{} ",msg.getAwemeId(),e.getMessage());
            return;
        }
        if(bloggerAwemesResponse.getStatus_code() == 2053){
            awemeRepository.updateAwemeIsAbleByAwemeId(0,awemeId);
            log.info("CheckAwemeExistEvent 视频确认失效，awemeId: {} ",msg.getAwemeId());
        }
    }

}
